-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
require/import vs require/include in PHP Coding Standards Documentation. - Fix for #143 #144
base: master
Are you sure you want to change the base?
Conversation
Fixed: require/import => require/include
Per @jrfnl's suggestion, made clear that autoloading only applies to classes.
Fixed missing space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching that typo @apermo! I've left a small suggestion inline. Let me know what you think.
wordpress-coding-standards/php.md
Outdated
@@ -605,7 +605,7 @@ Group `use` statements are available from PHP 7.0, and trailing commas in group | |||
[/alert] | |||
|
|||
[info] | |||
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load whatever is being imported. You'll either need to set up autoloading or load the file containing the class/function/constant using a `require/import` statement, for the imported constructs to be loaded when used. | |||
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require` or `include`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require` or `include`. | |
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using a `require/include[_once]` statement. Autoloading is only applicable to classes; for functions and constants, you must always use `require[_once]` or `include[_once]`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I definitely like the added [_once] in the new sentence. I'll already apply that second part.
I am personally unsure about the require/include[_once]
.
I personally read it as "'require/includestatement", and due to that "statement" I included the optional
_once` for both.
If you want to make it clear, I personally would suggest:
[...] or load the file containing the class using require[_once]
or include[_once]
. [...]
Imho that improves readability. Otherwise I personally would keep require/include
.
Your thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrfnl I've added the before mentioned change. And changed order to alphabetical (first include then require) this matches the paragraph on "Writing include/require statements" in lines 81-83 of the same file.
Let me know your thoughts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The order was intentional - require
should generally be preferred over include
, which is why it was mentioned first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrfnl I agree with that point. What do you think about adjusting the other appearance of include/require to require/include, to match the same idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd need to look at the complete doc to be sure (on the road now, so can't look), but in principle, I'd be open to such a change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jrfnl I've updated the order, and I've added a subsentence to the documentation in the upper paragraph. My experience showed that include
statements can result in silent malfunctions, which are a pain to track down. If you disagree adding this, I'm absolutely fine to omit that.
Applied suggestion from code review by @jrfnl
Further code review feedback. Changed order to alphabetikal order: include, require. To be consistent with Lines 81-83 (Writing include/require statements) and
Switched from alphabetical order, to recommendation order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@apermo Sorry for the slow turn-around. I've had another critical look and I think this still needs more tweaking, largely because the PR is trying to do more than just fix the issue you reported.
@@ -605,7 +605,7 @@ Group `use` statements are available from PHP 7.0, and trailing commas in group | |||
[/alert] | |||
|
|||
[info] | |||
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load whatever is being imported. You'll either need to set up autoloading or load the file containing the class/function/constant using a `require/import` statement, for the imported constructs to be loaded when used. | |||
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using `require[_once]` or `include[_once]` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require[_once]` or `include[_once]`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be some confusion in this sentence between singular and plural - "load referenced classes" versus "the file containing the class".
I think this still needs some finetuning.
Also, while I understand the change from "whatever is being imported" to "referenced classes", it disregards that more can be imported via a use
statement, including namespaces.
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load referenced classes. You'll either need to set up autoloading or load the file containing the class using `require[_once]` or `include[_once]` statement, for the imported classes to be loaded when used. Autoloading is only applicable to classes; for functions and constants, you must always use `require[_once]` or `include[_once]`. | |
Note that, unless you have implemented [autoloading](https://www.php.net/manual/en/language.oop5.autoload.php), the `use` statement won't automatically load whatever is being imported. For OO constructs, you'll either need to set up autoloading or load the file(s) containing the OO declaration(s) using `require[_once]` or `include[_once]` statements. | |
Autoloading is only applicable to OO constructs; for functions and constants, you must always use `require[_once]` or `include[_once]`. |
This may need a check how OO constructs are referred to elsewhere in the document - I'm not sure we make it explicit that this means "classes, interfaces, traits and enums" and/or whether that needs to be repeated.
Also, the above is still confusing if read strictly with a technical mindset, as autoloading === loading the file containing the OO declarations using require[_once]
or include[_once]
statements. It is just a way of doing that "just in time"/only when the file is needed.
### Writing include/require statements | ||
### Writing require/include statements | ||
|
||
Because `include[_once]` and `require[_once]` are language constructs, they do not need parentheses around the path, so those shouldn't be used. There should only be one space between the path and the include/require keywords. | ||
Because `require[_once]` and `include[_once]` are language constructs, they do not need parentheses around the path, so those shouldn't be used. There should only be one space between the path and the require/include keywords. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the order needs to change here as the paragraph directly below it points out that require
should be preferred over include
.
Not a blocker though.
|
||
It is _strongly recommended_ to use `require[_once]` for unconditional includes. When using `include[_once]`, PHP will throw a warning when the file is not found but will continue execution, which will almost certainly lead to other errors/warnings/notices being thrown if your application depends on the file loaded, potentially leading to security leaks. For that reason, `require[_once]` is generally the better choice as it will throw a `Fatal Error` if the file cannot be found. | ||
It is _strongly recommended_ to use `require[_once]` for unconditional includes. When using `include[_once]`, PHP will throw a warning when the file is not found but will continue execution, which will almost certainly lead to other errors/warnings/notices being thrown if your application depends on the file loaded, potentially leading to security leaks, or result in silent malfunctions which are hard to track down. For that reason, `require[_once]` is generally the better choice as it will throw a `Fatal Error` if the file cannot be found. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the addition adds value in this way and it distracts from/downplays the point about the risk of security leaks.
Errors are already mentioned earlier in the sentence. I'm open to rephrasing that part, but the way it is phrased now, the "For that reason" at the start of the next sentence would predominantly apply to the "silent malfunctions", while it is - predominantly - about the risk of security leaks.
This fixes #143
Also taken care of @jrfnl 's comment on slack that autoloading only applies to classes and not to functions or constants.